package com.party.mobile.web.controller.member;

import com.party.authorization.annotation.Authorization;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.notify.IMemberSetService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.member.MemberBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.member.output.MemberSetOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户消息开关控制器
 *
 * @author yifeng
 * @date 2018/6/27 0027
 * @time 下午 18:15
 */
@Controller
@RequestMapping("member/memberSet")
public class MemberSetController {
    @Autowired
    private MemberBizService memberBizService;
    @Autowired
    private CurrentUserBizService currentUserBizService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private IMemberSetService memberSetService;

    /**
     * 获取push消息开关接口
     *
     * @param request
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "getMemberSetInfo")
    public AjaxResult getMemberSetInfo(HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            MemberSetOutput memberSetOutput = memberBizService.getMemberSetInfo(currentUser.getId());
            return AjaxResult.success(memberSetOutput);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
    }

    /**
     * 设置jpush开关
     *
     * @param switchResult 选择结果
     * @param type         开关类型 1：活动 2：众筹报名 3：众筹成功 4：重要通知
     * @param request
     * @return
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "updateMemberSetInfo", method = RequestMethod.POST)
    public AjaxResult updateMemberSetInfo(Integer switchResult, Integer type, HttpServletRequest request) {
        //参数验证
        if (switchResult == null || type == null) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "参数错误");
        }
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            MemberSetOutput memberSetOutput = memberBizService.updateMemberSetInfo(switchResult, type, currentUser.getId());
            return AjaxResult.success(memberSetOutput);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        }
    }

    /**
     * 初始化用户消息设置
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("initPartnerMemberSet")
    public AjaxResult initPartnerMemberSet() {
        List<String> memberIdList = memberGroupService.getAllPartnerIds();
        for (String memberId : memberIdList) {
            memberSetService.initMemberSet(memberId);
        }
        return AjaxResult.success();
    }
}
